package com.example.hu.UI;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.csht.facelib.manager.CshtCompareUtil;
import com.csht.facelib.utils.DeviceIdUtils;
import com.csht.facelib.utils.SpUtils;
import com.csht.facelib.utils.ToastUtils;
import com.example.hu.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;

public class FaceAuthActivity extends BaseActivity {
    private Handler handler = new Handler();
    private EditText et_key;
    private boolean isAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_auth);
        init();
        initAuth();
    }



    private void initAuth() {
        if (isAuth){
            ToastUtils.showToast("正在自动授权");
            return;
        }
        isAuth = true;
        ToastUtils.showToast("自动授权中，请稍后");
        HashMap<String,String> map = new HashMap<>();
        map.put("appid",getPackageName());
        map.put("snh",DeviceIdUtils.getDeviceId());
        String postString = new JSONObject(map).toString();
        String url = "http://116.255.141.100:8026/cf/webapi/v1/device/apply";
        OkHttpUtils.postString().url(url).mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(postString)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        isAuth = false;
                        ToastUtils.showToast("授权失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        isAuth = false;
                        try {
                            JSONObject result = new JSONObject(response);
                            if ("1001".equals(result.optString("code"))){
                                String key = result.optString("msg");
                                et_key.setText(key);
                                SpUtils.setFaceSDKAuthCode(key);
                                initLicense();
                            } else {
                                ToastUtils.showToast("授权失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            ToastUtils.showToast("授权失败");
                        }
                    }
                });
    }

    private void init() {
        TextView tv_version = findViewById(R.id.tv_version);
        tv_version.setText("序列号："+DeviceIdUtils.getDeviceId());

        et_key = findViewById(R.id.et_key);

        findViewById(R.id.bt_on_active).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initAuth();
            }
        });

        findViewById(R.id.bt_on_line_active).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = et_key.getText().toString();
                if (TextUtils.isEmpty(key)){
                    ToastUtils.showToast("请输入授权码");
                } else {
                    SpUtils.setFaceSDKAuthCode(key);
                    initLicense();
                }
            }
        });
    }

    private void initLicense() {
        if (isAuth){
            ToastUtils.showToast("正在授权加载SDK");
            return;
        }
        isAuth = true;
        CshtCompareUtil.getInstance().init(new CshtCompareUtil.CallBack() {
            @Override
            public void onInitStart() {
                ToastUtils.showToast("初始化SDK中");
            }

            @Override
            public void onInitSuccess() {
                isAuth = false;
                ToastUtils.showToast("初始化SDK成功");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(FaceAuthActivity.this, LoginActivity.class));
                        finish();
                    }
                },1000);
            }

            @Override
            public void onInitFail() {
                isAuth = false;
                ToastUtils.showToast("初始化SDK失败");
            }

            @Override
            public void onInitAuthFail() {
                isAuth = false;
                ToastUtils.showToast("授权失败");
            }
        });
    }
}
